// pages/banghui/join.js
const app = getApp();
var util = require('../../utils/util.js')
var http = require('../../utils/server.js')

Page({
  data: {
    gangName: false
  },
  onLoad: function(option) {
    var that = this;
    const scene = decodeURIComponent(option.scene)
    var inviterId = scene || null
    that.setData({
      inviterId: inviterId
    }) 
    //用户信息
    app.getUserInfo(function(userInfo) {
      if (userInfo != null) {
        if (userInfo.gangId != '') {
          wx.setStorageSync('userInfo', null); 
          wx.reLaunch({
            url: '/pages/index/welcome'
          })
        }
        that.getData(userInfo)
      } else {
        that.setData({
          showAuthor: true
        })
      }
    })
  },
  onParentEvent: function(e) {
    app.loginServer(app.globalData.code, e.detail.encryptedData, e.detail.iv, this.getData)
  },
  getData: function(userInfo) {
    var that = this
    if (userInfo != null) {
      that.setData({
        showAuthor: false
      })
      http.post({
        url: "gang/getInviterInfo",
        inviter_id: that.data.inviterId
      }, function (data) {
        that.setData({
          inviterInfo: data
        })
      })
    } else {
      that.setData({
        showAuthor: true
      })
    }
  },
  formSubmit: function(e) {
    var that = this
    var formId = e.detail.formId;
    http.post({
      url: "gang/joinGang",
      inviter_id: that.data.inviterId,
      form_id: formId
    }, function(data) {
      util.showSuccess('加入成功', function () {
        wx.setStorageSync('userInfo', null); 
        wx.reLaunch({
          url: '/pages/index/welcome'
        })
      })
    })

  }
})